<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/common/base.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>

<style type="text/css">
 
        body{ padding:5px; margin:0; padding-bottom:15px;}
        #layout1{  width:100%;margin:0; padding:0;  } 
        .l-page-top{ height:40px; background:#ffffff; margin-bottom:3px;}
        h4{ margin:20px;}
.font_css 
{
      margin:0px;
      padding:0px;
      font-family:"微软雅黑","黑体","宋体";
	  color:#ffffff;
      font-size:15px;
      height:30px;
            
}
.font_css_a
{
      margin:0px;
      padding:0px;
      font-family:"微软雅黑","黑体","宋体";
	  color:#8f8f8f;
      font-size:15px;
      height:30px;
            
}
</style>
<script type="text/javascript">
/* $(document).ready(function(){//初始化
	$("#layout1").ligerLayout({ leftWidth: 180});
	$("#newGroupReport").hide();
    $("#newChildReport").hide();
    var annualIdOnload = $("#annualId").val();
    var groupIdOnload = $("#groupId").val();
    alert(annualIdOnload + "-----" + groupIdOnload);
    if(annualIdOnload != null && annualIdOnload != ''){
    	if(groupIdOnload != null && groupIdOnload != ''){
    		onloadReportAnnualByAnnualId(annualIdOnload);
    		againTreeLoad(annualIdOnload,groupIdOnload);
    		againDataGridLoad(groupIdOnload);
    	}else{
    		onloadReportAnnualByAnnualId(annualIdOnload);
        	loadTreeByAnnualId();
    	}
    }else{
    	onloadReportAnnual();
    }
}); */

//子报表保存成功后，刷新页面，需要重新加载树，并打开一级节点
function againTreeLoad(annualIdOnload,groupIdOnload){
	$.post('${ctx}/reportManageController/queryALLGroupAndReportByAnnual/'+annualIdOnload,function(data, textStatus) {
		if(textStatus == 'success') {
            treeNodes = data;   //把后台封装好的简单Json格式赋给treeNodes
            $.fn.zTree.init($("#dzzTree"), setting_annual_tree, treeNodes);
            var treeObj = $.fn.zTree.getZTreeObj("dzzTree");
            var nodes = treeObj.getNodes();
            treeObj.expandNode(nodes[0],true,false,true,true);
            var node = treeObj.getNodeByParam("groupOrReportId",groupIdOnload,null);
            treeObj.selectNode(node);
		}
	},"json");
}
function againDataGridLoad(groupIdOnload){
	$("#newGroupReport").hide();
	$("#newChildReport").show();
	$("#groupId").val(groupIdOnload);
	ligergrid = $("#groupOrReportGrid").ligerGrid({
        columns: [
          { display: '报表模板名称', name: 'templateName', width:'54%'},
          { display: '所属套标', name: 'name', width:'15%'},
         /*  { display: '子表说明', name: 'templateDescription', width:'39%'}, */
          { display: '创建时间', name: 'createdate', width:'10%'},
          {
              display: '操作', isAllowHide: false,width:'25%',
              render: function (row)
              {
            	  var myDate = new Date();
                  var html = '';
                	  html += '<a href="#" onclick="templateUpdate(\'' + row.templateId + '\')" class="update-btn">编辑</a>||';
                	  html += '<a href="#" onclick="templateDelete(\'' + row.templateId + '\')" class="update-btn">删除</a>||';
                	  html += '<a href="#" onclick="templateDetail(\'' + row.templateId + '\')" class="update-btn">明细</a>||';
                	  html += '<a href="#" onclick="templateCustomized(\'' + row.templateId + '\')" class="update-btn">报表定制</a>||';
                	  html += '<a href="#" onclick="onloadValidationFormula(\'' + row.templateId + '\')" class="update-btn">重新生成校核</a>';
                  return String.format(html,row.annualId);
              }
          }

        ],
        url: '${ctx}/reportManageController/templateTableData/'+groupIdOnload,
        pageSize: 10, sortName: 'orderId',
        width: '99%', height: '95%', rownumbers:true,
        pageParmName:'pageNum',
        pagesizeParmName:'pageSize',
        sortnameParmName:'sortname',
        sortorderParmName:'sortorder',
        alternatingRow:true,
	});
}

//页面进行增改删之后，重新加载，需要重新生成select下拉框，保证当前报表模板被选中
function onloadReportAnnualByAnnualId(annualId){
	$.ajax({
		url:'${ctx}/rptAnnualController/queryAllAnnualSelect',
		type:'POST',
		cache:false,
		success:function(data){
			var treeSelectIdOptions = "";
		    $.each(data,function(idx,item){
		    	if(item.annualId == annualId){
		    		treeSelectIdOptions += "<option value='"+item.annualId+"' selected>" + item.name + "</option>";
		    	}else{
		    		treeSelectIdOptions += "<option value='"+item.annualId+"'>" + item.name + "</option>";
		    	}
			});
		    $("#treeSelectId option").remove();
		    treeSelectIdFirst="<option value='0'>请选择</option>";
		    $(".treeSelectClass").append(treeSelectIdFirst).append(treeSelectIdOptions);
		}
	});
}

//加载报表版本选择下拉框
function onloadReportAnnual(){
	$.ajax({
		url:'${ctx}/rptAnnualController/queryAllAnnualSelect',
		type:'POST',
		cache:false,
		success:function(data){
			var treeSelectIdOptions = "";
		    $.each(data,function(idx,item){
		    	treeSelectIdOptions += "<option value='"+item.annualId+"'>" + item.name + "</option>";
			});
		    $("#treeSelectId option").remove();
		    treeSelectIdFirst="<option value='0'>请选择</option>";
		    $(".treeSelectClass").append(treeSelectIdFirst).append(treeSelectIdOptions);
		}
	});
}

//通过报表版本ID加载当前版本的报表树
var setting_annual_tree = {  
        data: {  
               simpleData: {  
                   enable: true,  
                   idKey: "groupOrReportId",  
                   pIdKey: "pid",  
                   rootPId: 1
               },
			   key:{
				 name:"groupOrReportName",
			   },
           },  
	    showLine : true,
        check: {
		enable: false
	    },              //每个节点上是否显示 CheckBox  
		isSimpleData : true,
		callback: {
			onClick: zTreeOnClick
		}
    };
//第一次进入当前页面时，加载该方法()
function loadTreeByAnnualId(){
	var annualId = $("#treeSelectId").val();
	if(annualId == null || annualId == ''){
		annualId = $("#annualId").val();
	}
	$("#annualId").val(annualId);
	$.post('${ctx}/reportManageController/queryALLGroupAndReportByAnnual/'+annualId,function(data, textStatus) {
		if(textStatus == 'success') {
            treeNodes = data;   //把后台封装好的简单Json格式赋给treeNodes
            $.fn.zTree.init($("#dzzTree"), setting_annual_tree, treeNodes);
            var treeObj = $.fn.zTree.getZTreeObj("dzzTree");
            var node = treeObj.getNodes();
            treeObj.expandNode(node[0],true,false,true,true);
            reloadGrid(node[0].groupOrReportId,annualId);
            $("#newGroupReport").show();
            $("#newChildReport").hide();
		}
	},"json"); 
}
//重新加载表格
function reloadGrid(groupOrReportId,annualId){
	ligergrid = $("#groupOrReportGrid").ligerGrid({
        columns: [
          { display: '套标名称', name: 'name', width:'25%'},
          { display: '套标具体说明', name: 'description', width:'59%'},
          /* { display: '套表类型', name: 'grptype',width:'10%',render:function(row){
			  var grptypeMap = {'1':'基层表','2':'汇总表','3':'通用表'};
        	  return grptypeMap[row.grptype + ''];
          }}, */
          {
              display: '操作', isAllowHide: false,width:'15%',
              render: function (row)
              {
            	  var myDate = new Date();
                  var html = '';
                	  html += '<a href="#" onclick="groupUpdate(\'' + row.groupId + '\')" class="update-btn">编辑</a>||';
                	  html += '<a href="#" onclick="deletegroup(\'' + row.groupId + '\')" class="update-btn">删除</a>||';
                	  html += '<a href="#" onclick="groupDetail(\'' + row.groupId + '\')" class="update-btn">明细</a>';
                  return String.format(html,row.annualId);
              }
          }

        ],
        url: '${ctx}/reportManageController/groupTableData/'+annualId,
        pageSize: 10, sortName: 'orderId',
        width: '99%', height: '96%', rownumbers:true,
        pageParmName:'pageNum',
        pagesizeParmName:'pageSize',
        sortnameParmName:'sortname',
        sortorderParmName:'sortorder',
        alternatingRow:true,
    });
}
//点击树节点生成不同的表格
function zTreeOnClick(event, treeId, treeNode) {
	$("#newGroupReport").show();
	$("#newChildReport").hide();
	if(treeNode.isGroupRpt == 2){
		var annualId = $("#treeSelectId").val();
		ligergrid = $("#groupOrReportGrid").ligerGrid({
	        columns: [
	          { display: '套标名称', name: 'name', width:'25%'},
	          { display: '套标具体说明', name: 'description', width:'59%'},
	          /* { display: '套表类型', name: 'grptype',width:'10%',render:function(row){
				  var grptypeMap = {'1':'基层表','2':'汇总表','3':'通用表'};
	        	  return grptypeMap[row.grptype + ''];
	          }}, */
	          {
	              display: '操作', isAllowHide: false,width:'15%',
	              render: function (row)
	              {
	            	  var myDate = new Date();
	                  var html = '';
	                	  html += '<a href="#" onclick="groupUpdate(\'' + row.groupId + '\')" class="update-btn">编辑</a>||';
	                	  html += '<a href="#" onclick="deletegroup(\'' + row.groupId + '\')" class="update-btn">删除</a>||';
	                	  html += '<a href="#" onclick="groupDetail(\'' + row.groupId + '\')" class="update-btn">明细</a>';
	                  return String.format(html,row.annualId);
	              }
	          }

	        ],
	        url: '${ctx}/reportManageController/groupTableData/'+annualId,
	        pageSize: 10, sortName: 'orderId',
	        width: '99%', height: '96%', rownumbers:true,
	        pageParmName:'pageNum',
	        pagesizeParmName:'pageSize',
	        sortnameParmName:'sortname',
	        sortorderParmName:'sortorder',
	        alternatingRow:true,
	    });
	}else if(treeNode.isGroupRpt == 1){
		$("#newGroupReport").hide();
		$("#newChildReport").show();
		$("#groupId").val(treeNode.groupOrReportId);
		ligergrid = $("#groupOrReportGrid").ligerGrid({
	        columns: [
	          { display: '报表模板名称', name: 'templateName', width:'54%'},
	          { display: '所属套表', name: 'name', width:'10%'},
	          /* { display: '子表说明', name: 'templateDescription', width:'39%'}, */
	          { display: '创建时间', name: 'createdate', width:'10%'},
	          {
	              display: '操作', isAllowHide: false,width:'25%',
	              render: function (row)
	              {
	            	  var myDate = new Date();
	                  var html = '';
	                  html += '<a href="#" onclick="templateUpdate(\'' + row.templateId + '\')" class="update-btn">编辑</a>||';
                	  html += '<a href="#" onclick="templateDelete(\'' + row.templateId + '\')" class="update-btn">删除</a>||';
                	  html += '<a href="#" onclick="templateDetail(\'' + row.templateId + '\')" class="update-btn">明细</a>||';
                	  html += '<a href="#" onclick="templateCustomized(\'' + row.templateId + '\')" class="update-btn">报表定制</a>||';
                	  html += '<a href="#" onclick="onloadValidationFormula(\'' + row.templateId + '\')" class="update-btn">重新生成校核</a>'
                	  /*  html += '<a href="#" onclick="templateComment(\'' + row.templateId + '\')" class="update-btn">批注定制</a>'; */
	                  return String.format(html,row.annualId);
	              }
	          }

	        ],
	        url: '${ctx}/reportManageController/templateTableData/'+treeNode.groupOrReportId,
	        pageSize: 10, sortName: 'orderId',
	        width: '99%', height: '95%', rownumbers:true,
	        pageParmName:'pageNum',
	        pagesizeParmName:'pageSize',
	        sortnameParmName:'sortname',
	        sortorderParmName:'sortorder',
	        alternatingRow:true,
	    });
	}else{
		$("#newGroupReport").hide();
		$("#newChildReport").show();
		return false
	}
};
//新增套表
function onNewGroupReport(){
	var annualId = $("#treeSelectId").val();
	$.layer({
	    type: 2,
	    maxmin: true,
	    shadeClose: true,
	    title: '新增套表',
	    shade: [0.1,'#000000'],
	    shift:'top',
	    offset: [($(window).height() / 20)+'px',''],
	    area: ['800px', '400px'],
	    iframe: {src: '${ctx}/reportManageController/preCreateReportGroup/'+annualId}
	}); 
}
//套表更新
function groupUpdate(groupId){
	var annualId = $("#treeSelectId").val();
	$.layer({
	    type: 2,
	    maxmin: true,
	    shadeClose: true,
	    title: '修改套表',
	    shade: [0.1,'#000000'],
	    shift:'top',
	    offset: [($(window).height() / 20)+'px',''],
	    area: ['800px', '400px'],
	    iframe: {src: '${ctx}/reportManageController/preUpdate/'+groupId + '/'+annualId}
	}); 
}
//删除套表
function deletegroup(groupId){
	var annualId = $("#treeSelectId").val();
	layer.confirm('是否删除套标，如果删除的套标下有子表，那么子表数据将随套标一起删除！',function(){
		$.ajax({
			url:'${ctx}/reportManageController/deleteReportGroup/' + groupId + '/'+annualId,
			type:'POST',
			cache:false,
			success:function(data){
					layer.alert('操作成功',9);
					$("#annualId").val(data);
					againData(data,"");
					//location.reload();
			}
		});
	});
}
//套表明细
function groupDetail(groupId){
	$.layer({
	    type: 2,
	    maxmin: true,
	    shadeClose: true,
	    title: '套表明细',
	    shade: [0.1,'#000000'],
	    shift:'top',
	    offset: [($(window).height() / 20)+'px',''],
	    area: ['800px', '400px'],
	    iframe: {src: '${ctx}/reportManageController/detail/'+groupId}
	}); 
}
//新增子报表
function onNewChildReport(){
	var annualId = $("#treeSelectId").val();
	var groupId = $("#groupId").val();
	$.layer({
	    type: 2,
	    maxmin: true,
	    shadeClose: true,
	    title: '新增子报表',
	    shade: [0.1,'#000000'],
	    shift:'top',
	    offset: [($(window).height() / 20)+'px',''],
	    area: ['800px', '400px'],
	    iframe: {src: '${ctx}/reportTemplateManageController/preCreate/'+groupId+'/'+annualId}
	}); 
}
//子报表明细
function templateDetail(templateId){
	$.layer({
	    type: 2,
	    maxmin: true,
	    shadeClose: true,
	    title: '子报表明细',
	    shade: [0.1,'#000000'],
	    shift:'top',
	    offset: [($(window).height() / 20)+'px',''],
	    area: ['800px', '400px'],
	    iframe: {src: '${ctx}/reportTemplateManageController/templateDetail/'+templateId}
	}); 
}

//删除子报表
function templateDelete(templateId){
	var annualId = $("#treeSelectId").val();
	var groupId = $("#groupId").val();
	layer.confirm('是否删除',function(){
		$.ajax({
			url:'${ctx}/reportTemplateManageController/reportTemplateDelete/' + templateId + "/" +annualId + "/" + groupId,
			type:'POST',
			cache:false,
			success:function(data){
					layer.alert('操作成功',9);
					againData(data[0],data[1]);
					//location.reload();
			}
		});
	});
}

//子报表修改
function templateUpdate(templateId){
	var annualId = $("#treeSelectId").val();
	var groupId = $("#groupId").val();
	$.layer({
	    type: 2,
	    maxmin: true,
	    shadeClose: true,
	    title: '子报表修改',
	    shade: [0.1,'#000000'],
	    shift:'top',
	    offset: [($(window).height() / 20)+'px',''],
	    area: ['800px', '400px'],
	    iframe: {src: '${ctx}/reportTemplateManageController/preUpdateTemplate/'+templateId + "/" +annualId + "/" + groupId}
	}); 
}
function againData(annualId,groupId){
	var annualIdOnload = annualId;
	var groupIdOnload = groupId;
	if(annualIdOnload != null && annualIdOnload != ''){
    	if(groupIdOnload != null && groupIdOnload != ''){
    		onloadReportAnnualByAnnualId(annualIdOnload);
    		againTreeLoad(annualIdOnload,groupIdOnload);
    		againDataGridLoad(groupIdOnload);
    	}else{
    		onloadReportAnnualByAnnualId(annualIdOnload);
        	loadTreeByAnnualId();
    	}
    }else{
    	return false;
    }
}

//弹出报表设计器
function templateCustomized(templateId){
	var annualId = $("#annualId").val();
	parent.$.layer({
		type: 2,
	    maxmin: true,
	    shadeClose: true,
	    title: '报表设计',
	    shade: [0.1,'#000000'],
	    shift:'top',
	    offset: [($(window).height() /100)+'px',''],
	    area: ['1350px', '650px'],
	    iframe: {src: '${ctx}/reportDesignController/preReportDesign/'+templateId+'/'+annualId}
    });
	//window.open('${ctx}/reportDesignController/preReportDesign/'+templateId);
}
//弹出报表设计器
function templateComment(templateId){
	var annualId = $("#annualId").val();
	parent.$.layer({
		type: 2,
	    maxmin: true,
	    shadeClose: true,
	    title: '报表批注',
	    shade: [0.1,'#000000'],
	    shift:'top',
	    offset: [($(window).height() / 50)+'px',''],
	    area: ['1300px', '600px'],
	    iframe: {src: '${ctx}/reportDesignController/preReportCommentDesign/'+templateId+'/'+annualId}
    });
	//window.open('${ctx}/reportDesignController/preReportDesign/'+templateId);
}

function onloadValidationFormula(templateId){
	var annualId = $("#annualId").val();
	layer.confirm('是否重新生成校核公式',function(){
		var index = layer.load("正在重新生成,请稍等……");
		$.ajax({
			url:'${ctx}/reportTemplateValidationController/onloadValidationFormual/' + templateId + "/" +annualId,
			type:'POST',
			cache:false,
			success:function(data){
				layer.close(index);
				layer.alert(data[0],1);
				ligergrid.loadData();
			}
		});
	});
}
</script>
</head>
<body>
<input type="hidden" id="annualId" name="annualId" value="${annualId}"/>
<input type="hidden" id="groupId" name="groupId" value="${groupId}"/>
<div id="layout1" style="height: 98%;">
      <div position="left" title="报表列表" style="height: 100%;width:100%;">
      		<div  style="text-align: center;">
				选择报表版本
			</div>
			<div  style="text-align: center;">
				<select id="treeSelectId" style="width:150px;margin-left:5px;" class="treeSelectClass" onchange="loadTreeByAnnualId();"></select>
			</div>
			<div style="text-align: center;OVERFLOW-Y: auto; OVERFLOW-X:hidden;height:80%;">
				<ul id="dzzTree" class="ztree"></ul>
			</div>
	  </div>
      <div position="center" title="报表详细列表">
      	<div style="margin-right:15px;margin-top:5px;padding:5px;border:0px solid green;position:relative;">
			<input type="button" class="btn btn-info" value="新增套表" onclick="onNewGroupReport();" id="newGroupReport"/>
			<input type="button" class="btn btn-info" value="新增子表" onclick="onNewChildReport();" id="newChildReport"/>
		</div>
      	<div id="groupOrReportGrid" style="margin:5px; padding:0;height:90%;width:100%" ></div>
		<div style="right:5px;padding:0px; bottom:0px; position:absolute;">
		<!--<div style="text-align: right;margin-right:10px;line-height:20px; bottom:0; left:0;font-size:small; padding:6px; position:absolute;">-->
		</div>
      </div>  
</div> 
</body>
<script type="text/javascript">
$(document).ready(function(){//初始化
	$("#layout1").ligerLayout({ leftWidth: 180});
	$("#newGroupReport").hide();
    $("#newChildReport").hide();
    onloadReportAnnual();
});
</script>
</html>